<template>
  <el-dialog
    title="批量生成"
    :close-on-click-modal="false"
    width="30%"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="是否启用" prop="enable">
        <el-switch
          v-model="dataForm.enable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item label="所属品牌" prop="brandId">
        <el-cascader :options="options" v-model="selectedOptions"></el-cascader>
      </el-form-item>
      <el-form-item label="类别" prop="tokenUsing">
        <el-radio v-model="dataForm.tokenUsing" label="0" border size="medium"
          >存储箱</el-radio
        >
        <el-radio v-model="dataForm.tokenUsing" label="1" border size="medium"
          >背包</el-radio
        >
      </el-form-item>
      <el-form-item label="生成个数">
        <el-input-number
          v-model="count"
          :min="0"
          :max="200"
          label="生成个数"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      //consumerid
      selectedOptions: [],
      cid: 0,
      count: 0,
      options: [], //级联选择
      visible: false,
      dataForm: {
        runToken: 0,
        masterId: "",
        enable: "",
        brandId: "",
        sceneId: "",
        tokenUsing: "",
      },
      dataRule: {},
    };
  },
  methods: {
    init(id) {
      this.cid = this.$cookie.get("userid");

      this.$nextTick(() => {
        // this.$refs['dataForm'].resetFields()//重置表单

        this.$http({
          url: this.$http.adornUrl(`/product/conbrasce/tree/${this.cid}`),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.tree;
          }
        });
      });
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/devtoken/add/${this.count}`),
            method: "post",
            data: {
              runToken: 1,
              masterId: Number(this.cid),
              enable: this.dataForm.enable,
              brandId: this.selectedOptions[0],
              sceneId: this.selectedOptions[1],
              tokenUsing: Number(this.dataForm.tokenUsing),
            },
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
